/**
 * Created by zp on 2016/10/3.
 */

define([],function(){
  'use strict';
  function ctrl($state,$http,$scope,$rootScope,$ionicLoading,$timeout,request){
    $scope.shadeDivHeight = $("ion-view")[0].clientHeight+"px";
    var enClick;
    $scope.userInfo = {
      firstname:'',
      lastname:'',
      email:'',
      phonenumber:'',
      password:'',
      password2:''
    }

//listen all input
    var registerwatch = $scope.$watch('userInfo',
      function(){

        if($scope.userInfo.firstname != '' && $scope.userInfo.firstname != undefined &&
          $scope.userInfo.lastname != '' && $scope.userInfo.lastname != undefined &&
          $scope.userInfo.email != '' && $scope.userInfo.email != undefined &&
          $scope.userInfo.phonenumber != '' && $scope.userInfo.phonenumber != undefined &&
          $scope.userInfo.password.length >=6 != '' &&
          $scope.userInfo.password == $scope.userInfo.password2){
          $('.registerbutton').css('background','#65c178');
          enClick = 1;
          registerwatch();
        }
      }
      ,true)




    $scope.register = function(){
      if(enClick == 1){
        $ionicLoading.show();
        var url = 'https://cartgogogo.com/custom/api/mobile/user.php';
        var data = "method=register&firstname="+$scope.userInfo.firstname+"&lastname="+$scope.userInfo.lastname+
          "&email="+$scope.userInfo.email+"&password="+$scope.userInfo.password+"&phonenumber="+$scope.userInfo.password;
        var promise = request.postreq(url,data);
        promise.then(
          function(success){
            $ionicLoading.hide();
            if(success.success == 0){
              //alert(success.return_code);
            }
          },
          function(error){
            $ionicLoading.hide();
          })
        $timeout(function(){
          $ionicLoading.hide();
        },3000)
      }
    }
  }
  ctrl.$inject = ['$state','$http','$scope','$rootScope','$ionicLoading','$timeout','request'];
  return ctrl;
})
